<?php
namespace app\souther\controller;

use app\common\model\Wares as WaresModel;
use app\common\model\Mold;
use think\Db;

class Wares extends Base
{
	
	function __construct()
	{
		parent::_initialize();
	}

    /**
     * [index 列表]
     * @return [type] [description]
     * @author [南風]
     */
    public function index()
    {
        $model = new WaresModel();

        if(request()->isAjax())
        {
            $data = input('param.');
            $map = [];

            if(is_array($data) && !empty($data['key'])){
                $map['title'] = ['like',"%" . $data['key'] . "%"];
            }
            $this->getPageAndSize($data);

            try{
                $count =$model->getWaresCountByCondition($map);
                $allpage = intval(ceil($count / $this->size));//总页面
                $lists = $model->getWaresByCondition($map, $this->from, $this->size);//列表
            }catch (\Exception $e)
            {
                writelog($e->getMessage(),-1);
                exception($e->getMessage());
            }

            writelog('查看商品列表');

            if(empty($lists))
                return '';

            return $this->fetch('ajax_list',[
                'count' => $count,//总记录数
                'allpage' => $allpage,
                'lists' => $lists,
            ]);
        }

        return $this->fetch('',['app'=>'wares']);
    }

    /**
     * [add 添加]
     * @return [type] [description]
     * @author [南風]
     */
    public function add()
    {
        
        if(request()->isAjax()){
            
            $model = new WaresModel();

            $param = input('post.');

            try{

                $id = $model->addData($param);

            }catch(\Exception $e){
                writelog($e->getMessage(),-1);
                return admin_json(-1, $e->getMessage());
            }
            writelog('添加商品--'.$id);
            return admin_json(1,'成功','',url('/wares'));
        }

        $model = new Mold();

        return $this->fetch('add',[
            'mold' => rule($model->getAllData()),
        ]);
    }

    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [南風]
     */
    public function edit()
    {
		$model = new WaresModel();
		
        if(request()->isPost()){
            
            try{
                $param = input('post.');
                $model->editData($param);
            }catch(\Exception $e){
                writelog($e->getMessage(),-1);
                return admin_json(-1,$model->getError());
            }
            writelog('编辑商品--'.$param['id']);
            return admin_json(1,'成功','',url('/wares'));
        }
        
		$mold = new Mold();

        $res = $model->findData(input('param.id'));

		$res['images'] = explode('#',substr($res["banner"],1));

        return $this->fetch('edit',[
            'edit' => $res,
            'mold'  =>  rule($mold->getAllData())
        ]);
    }

    /**
     * [del  删除]
     * @author [南風]
     * @return array
     */
    public function del()
    {
        $model = new WaresModel();

        $id = input('param.id');

        $model->deleteTrueData($id);

        writelog('删除商品--'.$id);

        return admin_json(1,'成功');
    }

    /**
     * [status 状态]
     * @author [南風]
     * @return array
     */
    public function status()
    {
        $model = new WaresModel();
        $data=input('param.');
        $res = $model->changeStatus(['id' => $data['id']],$data['status']);

        if($res)
        {
            if($data['status'] == 0)
            {
                writelog('禁用商品状态--'.$data['id']);
                $statusurl = url('status',['id'=>$data['id'],'status'=>1]);
                return admin_json(2,'已禁止',['statusurl'=>$statusurl]);
            }
            else
            {
                writelog('启用商品状态--'.$data['id']);
                $statusurl = url('status',['id'=>$data['id'],'status'=>0]);
                return admin_json(1,'已启用',['statusurl'=>$statusurl]);
            }
        }
        return admin_json(-1,'操作失败');
    }
}